package cn.jietuo.juc.c_008_interview1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/8
 * @description:
 */
public class T04_LockSupport {

    private List<Integer> list = new ArrayList<>();

    private void add(Integer i) {
        list.add(i);
    }

    private Integer size() {
        return list.size();
    }

    private static Thread t1 = null,t2 = null;

    public static void main(String[] args) {
        T04_LockSupport t = new T04_LockSupport();
        t2 = new Thread(()->{
            LockSupport.park();
            System.out.println("t2结束");
            LockSupport.unpark(t1);
        });

        t1 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                t.add(i);
                System.out.println("add:" + i);
                if (t.size() == 5){
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
        });

        t1.start();
        t2.start();
    }
}
